/**
 * 积分订单列表
 * author:zhangjlt
 * date:2016年3月10日10:30:42
 */
;
define(function(require, exports, module) {
	var wechat = require("wechat");
	var template = require("template");
	var creditOrder = {
		data: {},
		type: "1",
		currentPage: 1, //本月当前第几页
		pageSize: 20,
		loadedAllFlag: false,
		beginTime: "",
		endTime: "",
		/**
		 * bindEvent 页面事件
		 */
		bindEvent: function() {
			$(".orderType").unbind().on("click", function() {
				var type = $(this).attr("orderType");
				$(".orderType").removeClass("order_active")
				$(this).addClass("order_active");
				creditOrder.type = type;
				creditOrder.getDate(type);
				$("#main_creditOrder").html("");
				creditOrder.currentPage = 1;
				if($("#main_creditOrder" + type).children().length == 0) {
					creditOrder.loadData(type, creditOrder.currentPage);
				}

			});
			//返回
			$("#return_creditOrder").unbind().on("click", function() {
				window.location.href = "#/index/my";
			});
			$(document).off("click", '.credit_orderItem').on("click", '.credit_orderItem', function() {
				var orderId = $(this).attr("orderId");
				window.localStorage.setItem("creditorderId", orderId);
				window.location.href = "#/index/creditOrderDetail";
			});
		},
		getDate: function(type) {
			var myDate = new Date();
			var Month = creditOrder.zeroAdd(myDate.getMonth() + 1);
			var nowData = myDate.getFullYear() + '-' + Month + '-' + creditOrder.zeroAdd(myDate.getDate());
			var oldData = nowData;
			switch(type) {
				case 'month':
					oldData = creditOrder.addDate(myDate, -30);
					break;
				case 'threeMonth':
					oldData = creditOrder.addDate(myDate, -90);
					break;
				case 'all':
					oldData = nowData;
					break;
			}
			creditOrder.beginTime = oldData + " 00:00:00";
			creditOrder.endTime = nowData + " 23:59:59";
		},
		/**
		 * 进行页面路由监听配置
		 */
		bindRout: function() {
			//公共页面监听路由
			Path.map("#/index/:page_name").to(function() {
				console.log(this.params["page_name"]);
				var page_name = this.params["page_name"];
				//来自状态
				var from = "right";
				if(page_name == "my"){
					from = "left";
				}
				//记录状态
				common.loadPage({
					pageUrl: "html/" + page_name + ".html",
					reload: true,
					from: from
				});
			});
			
			Path.map("#home").to(function() {
				//返回首页
				common.loadPage({
					pageUrl: "html/index.html",
					reload: false,
					from: "left"
				});
			});
			//Path.root("#home");
			Path.listen();
		},
		
		/**
		 * 页面数据加载
		 */
		loadData: function(loadType, currentPage) {
			//当前页
			creditOrder.currentPage = currentPage;
			//计算日期
			creditOrder.getDate(loadType);
			var param = {};
			if(loadType == "all") {
				param = {
					"openId": window.localStorage.getItem("openId"),
					"appId": window.localStorage.getItem("appId"),
					"currentPage": creditOrder.currentPage,
					"pageSize": creditOrder.pageSize
				};
			} else {
				param = {
					"openId": window.localStorage.getItem("openId"),
					"appId": window.localStorage.getItem("appId"),
					"currentPage": creditOrder.currentPage,
					"pageSize": creditOrder.pageSize,
					"startTime": "2016-02-20 00:00:00",
					"endTime": "2018-03-22 23:59:59",
				};
			}
			common.loadData({
				url: "/wxmember/service/getCreditOrderList",
				param: param,
				successCallBack: creditOrder.getOrderListCallBack, //回调函数
				dataSource: "getOrderList",
				loadAll: creditOrder.loadedAllFlag
			});

		},
		//callback function
		getOrderListCallBack: function(json) {
			console.log("调用后台ajax返回值为：" + JSON.stringify(json));
			if(json.status == "0") {
				if(json.data && json.data.list && json.data.list.length > 0) {
					var html = template('creditOrderTemplate', json);
					$("#main_creditOrder").append(html);
					//iscroll滚动加入
					var iscroll = require("iscroll");
					common.initIscroll({
						container: "iscrollContent_creditOrder",
						showScrollBar: true,
						pullUpActionCallBack: creditOrder.pullUpActionCallBack,
						pullDownActionCallBack:creditOrder.pullDownActionCallBack,
						loadAll:creditOrder.loadedAllFlag
					});
				}else{
					if(creditOrder.currentPage == 1) {
						common.show_nodata("当前没有消费订单信息", "iscrollContent_creditOrder");
					}else{
						//两个部分共同控制加载全部
						common.loadedAll();
						myOrder.loadedAllFlag = true;
					}
				}

			} else {
				if(creditOrder.currentPage == 1) {
					common.show_nodata("当前没有消费订单信息", "iscrollContent_creditOrder");
				}

				common.showTip({
					content: json.message
				});
			}
		},
		pullUpActionCallBack: function() {
			creditOrder.currentPage += 1;
			creditOrder.loadData(creditOrder.type,creditOrder.currentPage);
		},
		pullDownActionCallBack:function(){
			creditOrder.currentPage = 1;
			$("#main_creditOrder").html("");
			creditOrder.loadedAllFlag=false;
			creditOrder.loadData();
		},
		addDate: function(date, days) {
			var d = new Date(date);
			d.setDate(d.getDate() + days);
			var m = d.getMonth() + 1;
			return d.getFullYear() + '-' + this.zeroAdd(m) + '-' + this.zeroAdd(d.getDate());
		},
		zeroAdd: function(s) {
			return s < 10 ? '0' + s : s;
		},
		/**
		 * each page init mothed
		 */
		initialize: function() {
			//初始化属性数据
			this.currentPage = 1;
			this.loadedAllFlag = false;
			//设置title样式
			if(common.isWeiXin()){
				$("#nav_creditOrder").css("top",0);
				$("#iscrollContent_creditOrder").css("top","1.8rem");
			}
			//动态加载数据
			creditOrder.loadData("month", 1);
			//事件绑定
			creditOrder.bindEvent();
			//路由绑定
			creditOrder.bindRout();
		}
	};

	module.exports = creditOrder;
});